using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Projekt4Now
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Player playerOne;
        Tools tools;

        private SpriteFont myFont;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.ApplyChanges();

            playerOne = new Player();
            tools = new Tools();
            playerOne.setLocation(new Vector2(20, 600));

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            myFont = Content.Load<SpriteFont>("myFont");
            
            playerOne.setTexture(Content.Load<Texture2D>("Images\\player"));

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            //Keyboard controls
            if (Keyboard.GetState().IsKeyDown(Keys.D))      // when D key is pressed
                playerOne.movePlayer(new Vector2(3, 0));    // move right
            if (Keyboard.GetState().IsKeyDown(Keys.A))      // when A key is pressed
                playerOne.movePlayer(new Vector2(-3, 0));   // move left
            if (Keyboard.GetState().IsKeyDown(Keys.W))      // when W key is pressed
                playerOne.acceleratePlayer(-0.2f);          // accelerate upwards
            else                                            // when the W key is not being pressed
                if (playerOne.getLocation().Y < 600) 
                    playerOne.acceleratePlayer(0.2f);           // deccelerate
            if (Keyboard.GetState().IsKeyDown(Keys.S))      
                playerOne.acceleratePlayer(0.2f);
            if(Keyboard.GetState().IsKeyDown(Keys.Space))   // when space bar is pressed
                playerOne.acceleratePlayer(-3.0f);          // instant boost to acceleration

            if (playerOne.getSpeed() >= 6.0f)               // when player reaches max speed downwards
                playerOne.setSpeed(6.0f);                   // reset max speed
            else if (playerOne.getSpeed() <= -6.0f)         // when player reaches max speed upwards
                playerOne.setSpeed(-6.0f);                  // reset max speed

            if (playerOne.getLocation().Y >= 600)                                       // if player reaches (imaginary) ground
            {
                playerOne.setLocation(new Vector2(playerOne.getLocation().X, 600));     // stop him from falling
                if (playerOne.getSpeed() >= 0.0f)
                    playerOne.setSpeed(0.0f);
            }

            playerOne.movePlayer(new Vector2(0, playerOne.getSpeed()));                 // execute the player's current vertical speed
            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend); //start drawing the 2d images
                spriteBatch.Draw(playerOne.getTexture(), playerOne.getLocation(), Color.White);
                tools.DrawFonts(spriteBatch, myFont, playerOne.getSpeed());
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
